<?php

class arcControl extends commonControl {

    function index() {
        $db = K("arc");
        $cat_where = isset($_GET['cat_id']) ? "arc.cat_id = '{$_GET['cat_id']}'" : "";
        $pid_where = isset($_GET['cat_id']) ? "category.cat_pid = '{$_GET['cat_id']}'" : "";
        $total = $db->where($cat_where)->count(); //总条数
        $page = new page($total, 10);
        $this->assign("page", $page->show(2));
        $rows = $db->where($cat_where)->limit($page->limit())->order("arc_sort")->all();
        $this->assign("arc", $rows);
        
        
        $dbs = M("category");
        $cat = $dbs->field("cat_name","cat_id","cat_pid")->all(); //查找所有栏目
        $category = arr::channel($cat, "cat_id", "cat_pid");
        $this->assign("category", $category);
        
        isset($_GET["cat_id"])?$cid=$_GET["cat_id"]:$cid="";
        $this->assign("cid", $cid);
        $this->display();
    }

    function add_show() {
        $db = M("category");
        $this->assign("sonid", C("SESSION_NAME"));
        $this->assign("cokval", $_COOKIE[C("SESSION_NAME")]);
        $this->assign("category", arr::channel($db->all(), "cat_id", "cat_pid"));
        $this->display();
        
        
    }

//添加文章
    function add() {
        $_POST['addtime'] = time();
        $_POST['admin_id'] = $_SESSION['admin_id'];
        $db = M("arc");

        if ($db->add()) {
            $this->success("增加文章成功", "index");
        } else {
            $this->error("文章添加失败", "index");
        }
    }

    function edit_show() {
        $db = M("arc");
        $arc = $db->find($_GET["arc_id"]);
        $this->assign("arc", $arc);
        $cat_id = $arc["cat_id"];
        $cat = arr::channel($db->table("category")->all(), "cat_id", "cat_pid");
        
        $this->assign("sonid", C("SESSION_NAME"));
        $this->assign("cokval", $_COOKIE[C("SESSION_NAME")]);
        $this->assign("cat", $cat);

        $this->display();
    }
    
    //上传图片
    function upload() {
        c("debug", 0);
        c("THUMB_ON",1);
        $upload = new upload();
        $file = $upload->upload();
        if ($upload) {
            $data = array("msg" => "success", "file" => $file[0]['path'],"thumb"=>$file[0]["thumb"]);
        } else {
            $data = array("msg" => "error");
        }
        echo json_encode($data);
    }
    //AJAX del
    function delpic() {
        C("debug", 0);
        $file=array();
        $file[0] = $_POST['path'];
        $file[1] = $_POST['rec'];
        $sta=1;
        foreach ($file as $v){
            unlink($v)?"":$sta=0;
        }
        echo $sta;
    }
    
    //编辑
    function edit() {
        $db = M("arc");
        if ($db->save() >= 0) {
            $this->success("修改成功", "index");
        } else {
            $this->error("修改失败");
        }
    }

    function del() {
        $db = M("arc");
        if ($db->del($_GET["arc_id"])) {
            $this->success("删除成功");
        } else {
            $this->error("删除失败");
        }
    }

    function ajaxEdit() {
        $db = M("arc");
        $db->update();
        echo $db->get_affected_rows();
    }
    
}

